<?php

namespace app\admin\controller\wdsxh;

use addons\wdsxh\library\Wxapp;
use app\common\controller\Backend;

/**
 * 活动报名
 *
 * @icon fa fa-circle-o
 */
class ActivityRefund extends Backend
{
    
    /**
     * ActivityRefund模型对象
     * @var \app\admin\model\wdsxh\ActivityRefund
     */
    protected $model = null;

//    protected $dataLimit = 'auth';
//    protected $dataLimitField = 'admin_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wdsxh\ActivityRefund;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $UsrtColumn =  (new \app\admin\model\wdsxh\Member())->column('uid');
            $list = $this->model
                    ->with(['user','wdsxhmember','wdsxhactivity','wdsxhactivityapply'])
                    ->where($where)
                    ->where(config('database.prefix').'wdsxh_activity_refund.user_id','in',$UsrtColumn)
                    ->where(config('database.prefix').'wdsxh_activity.pay_type','1')
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname','avatar']);
				$row->getRelation('wdsxhmember')->visible(['name','mobile','picture']);
				$row->getRelation('wdsxhactivity')->visible(['title']);
				$row->getRelation('wdsxhactivityapply')->visible(['id','money','status','createtime']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /*
     * 同意退款
     */
    public function agree($ids=null){
        $data=$this->model->get($ids);
        if(!$data){
            $this->error('退款信息不存在');
        }
        $user=\app\admin\model\User::where('id',$data->user_id)->field('id')->find();
        if(!$user){
            $this->error('用户信息不存在');
        }
        $map=array(
            'uid'=>$data->user_id,
            'items_id'=>$data->apply_id,
            'type'=>1,
            'status'=>'2'
        );
        $meps = array(
            'user_id'=>$data->user_id,
            'status' =>'2',
            'id'=>$data->apply_id,
        );
        $appyModel=\app\admin\model\wdsxh\ActivityApply::where($meps)->find();
        $order=\app\admin\model\wdsxh\Order::where($map)->find();
        if(!$order){
            $this->error('支付订单信息不存在');
        }
        $refundSn=wdsxh_create_order();
        $res=Wxapp::payRefund($order->order_sn,$refundSn,$data->money,array('refund_desc'=>'活动报名退款'));
        if($res && $res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS'){
            $data->status='1';
            $data->disposetime=time();
            $data->save();
            $order->refund_sn=$refundSn;
            $order->refund_time=time();
            $order->status='4';
            $order->save();
            $appyModel->status='3';
            $appyModel->save();
            $this->success('退款操作成功');
        }else{
            $this->error('退款失败,错误信息：'.$res['return_msg']);
        }
    }

    /*
     * 拒绝退款
     */
    public function refuse($ids=""){
        $data=$this->model->get($ids);
        if(!$data){
            $this->error('退款信息不存在');
        }
        $meps = array(
            'user_id'=>$data->user_id,
            'status' =>'2',
            'id'=>$data->apply_id,
        );
        $appyModel=\app\admin\model\wdsxh\ActivityApply::where($meps)->find();
        $data->status='2';
        $data->disposetime=time();
        $data->save();
        $appyModel->status='1';
        $appyModel->save();
        $this->success('操作成功');
    }
}
